import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import useUserStore from './userStore'
import { getQuarter } from '@/utils'

import { StatementStatus } from '@/components/EnergyDataReport/types'
import type { ReportColumnsItem } from '@/components/EnergyDataReport/types'
import { TABLE_ACCOUNT_CONFIGS } from '@/components/EnergyDataReport/config'

export function getStatementStatusText(status: StatementStatus): string {
  const obj = {
    [StatementStatus.UNDER_REPORT]: '未上报',
    [StatementStatus.STAGED]: '已暂存',
    [StatementStatus.REPORTED]: '已上报',
    [StatementStatus.RESOLVED]: '已通过',
    [StatementStatus.REJECTED]: '已驳回',
  }

  return obj[status]
}

/**
 * 根据表ID获取表名称
 */
export function getTableName(tId: number): string | void {
  for (const t of Object.values(TABLE_ACCOUNT_CONFIGS)) {
    if (t.ID === tId) {
      return t.NAME
    }
  }
}

/** 数据列配置扁平化 */
export function getFlattenColumns(columns: ReportColumnsItem[]) {
  const getItems = (arr: ReportColumnsItem[]) => {
    const result = [];
    (function callback(arr) {
      for (let i = 0; i < arr.length; i++) {
        const v = arr[i]
        if (v) {
          const { children, ...rest } = v
          if (children && children.length > 0) {
            callback(children)
          }
          else if (!rest.hidden) {
            result.push({ ...rest })
          }
        }
      }
    })(arr)
    return result
  }
  return getItems(columns)
}

const useStatementStore = defineStore('statementStore', () => {
  const year = ref(new Date().getFullYear())
  const quarter = ref(getQuarter())

  const isAnnual = computed(() => {
    return quarter.value === 5
  })

  const userStore = useUserStore()
  // fix: tenantId/tenantName may be null
  const enterpriseId = computed(() => userStore.userInfo.tenantId || '')
  const enterpriseName = computed(() => userStore.userInfo.tenantName || '')

  const statementId = ref<number | null>(null)
  const statementName = ref<string>('')
  const statementStatus = ref<StatementStatus>(StatementStatus.UNDER_REPORT)
  const statementColumns = shallowRef<ReportColumnsItem[]>([])

  const statementFlattenColumns = computed(() =>
    getFlattenColumns(statementColumns.value),
  )

  const readonly = ref(false)

  const visible = ref(false)

  function setYear(value: number) {
    year.value = value
  }

  function setQuarter(value: number) {
    quarter.value = value
  }

  function setStatementId(value: number) {
    statementId.value = value
  }

  function setStatementName(value: string) {
    statementName.value = value
  }

  function setStatementColumns(value: ReportColumnsItem[]) {
    statementColumns.value = value
  }

  function setStatementStatus(value: StatementStatus) {
    statementStatus.value = value
  }

  function setReadonly(value: boolean) {
    readonly.value = value
  }

  function setVisible(value: boolean) {
    visible.value = value
  }

  return {
    /** 填报年份 */
    year,
    /** 填报季度（年报值为5） */
    quarter,
    /** 能源企业ID */
    enterpriseId,
    /** 能源企业名称 */
    enterpriseName,
    /** 报表ID */
    statementId,
    /** 报表名称 */
    statementName,
    /** 报表数据列配置 */
    statementColumns,
    /** 扁平化的报表数据列配置 */
    statementFlattenColumns,
    /** 填报状态 */
    statementStatus,
    /** 报表是否只读 */
    readonly,
    /** 是否为年报 */
    isAnnual,
    /** 右键菜单显示与隐藏 */
    visible,

    /** 填报年份 */
    setYear,
    /** 填报季度（年报值为5） */
    setQuarter,
    /** 报表ID */
    setStatementId,
    /** 报表名称 */
    setStatementName,
    /** 报表数据列配置 */
    setStatementColumns,
    /** 填报状态 */
    setStatementStatus,
    /** 报表是否只读 */
    setReadonly,
    /** 右键菜单显示与隐藏 */
    setVisible,
  }
})

export default useStatementStore
